/**
 * web服务入口类
 * @author yangxiao
 * @module app/web/index
 */

import * as express from "express";
import * as expressSession from "express-session";
import * as fs from 'fs';
import * as path from 'path';
import * as serverConfig from '../../lib/config/config';
import * as getClass from '../../util/get-class';

export function main(){

    let app = express();
    let sessionParser = expressSession({
        secret:"secret",
        resave: true,
        saveUninitialized: true
    });

    app.use(sessionParser);
    //启动文档服务器(开启httpServer) http://127.0.0.1:4003/api
    app.set('views',path.normalize( serverConfig.getAssetPath()+ '/html/views/'));
    app.set("view engine","ejs");
    app.use(express.static('asset/html/')); //web所需的css,js,png等资源的存放路径
    app.use(express.static('docs/')); //部署教学文档 http://localhost:8080/apidoc
    app.all("/webservice/*/*",(req,res)=>{
        let moduleName = req.originalUrl.split("/")[2];
        let functionName = req.originalUrl.split("/")[3];
        let exists = fs.existsSync(__dirname + path.normalize("/"+moduleName+"/index.js"));
        let exc = getClass.getModuleFunction(path.normalize(moduleName+"/index.js"),__dirname,functionName);
        if(exc){
            exc(req,res);
        }
    });
    let httpServer =app.listen(8080,"0.0.0.0", function () { console.log('Listening on http' , httpServer.address().address, httpServer.address().port) });
    
}
